﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace IT_Pro.Forms
{
    public partial class frmCVBanGiaoLinhKien : DevExpress.XtraEditors.XtraForm
    {
        public frmCVBanGiaoLinhKien()
        {
            InitializeComponent();
        }

        public frmCVBanGiaoLinhKien(string nguoinhan,string tennguoinhan)
        {
            InitializeComponent();
            loaddsdulieulenForm(nguoinhan, tennguoinhan);
        }

        private void gridItem_Click(object sender, EventArgs e)
        {
            
        }

        private void frmCVBanGiaoLinhKien_Load(object sender, EventArgs e)
        {
            dtthemlk.Columns.Add("MaLK", Type.GetType("System.String"));
            dtthemlk.Columns.Add("NhaCC", Type.GetType("System.String"));
            dtthemlk.Columns.Add("HanBaoHanh", Type.GetType("System.DateTime"));
            dtthemlk.Columns.Add("TinhTrang", Type.GetType("System.String"));
            dtthemlk.Columns.Add("ID", Type.GetType("System.String"));

            dtIT.Columns.Add("STT", Type.GetType("System.String"));
            dtIT.Columns.Add("MaLK", Type.GetType("System.String"));
            dtIT.Columns.Add("TenLK", Type.GetType("System.String"));
            dtIT.Columns.Add("NhaCC", Type.GetType("System.String"));
            dtIT.Columns.Add("HanBaoHanh", Type.GetType("System.DateTime"));
            dtIT.Columns.Add("TinhTrang", Type.GetType("System.String"));
            dtIT.Columns.Add("ID", Type.GetType("System.String"));

        }

        private void loaddsdulieulenForm(string nguoinhan,string tennguoinhan)
        {
            txtMaNguoiNhan.Text=nguoinhan;
            txtTenNguoiNhan.Text=tennguoinhan;
            DataTable dt = Class.CV_BanGiaoThietBi.HienThiDanhSachBanGiao(nguoinhan);

            if (dt.Rows.Count > 0)
            {
                cboMaMay.Properties.DataSource = dt;
                cboMaMay.EditValue = dt.Rows[0]["MaMay"].ToString();
                btnAdd.Enabled = true;
            }
            else
            {
                btnAdd.Enabled = false;
            }

            cboNguoiGiao.Properties.DataSource = Class.NV_NhanVien.HienThiDsNhanVien();
            cboNguoiGiao.Properties.DisplayMember = "TenNV";
            cboNguoiGiao.Properties.ValueMember = "MaNV";
            cboNguoiGiao.EditValue = Class.DataServices.Manv;

            cboNguoiNhanIT.Properties.DataSource = Class.NV_NhanVien.HienThiDsNhanVien();
            cboNguoiNhanIT.Properties.DisplayMember = "TenNV";
            cboNguoiNhanIT.Properties.ValueMember = "MaNV";
            cboNguoiNhanIT.EditValue = Class.DataServices.Manv;

            cboNguoiGiaoIT.Properties.DataSource = Class.NV_NhanVien.HienThiDsNhanVien();
            cboNguoiGiaoIT.Properties.DisplayMember = "TenNV";
            cboNguoiGiaoIT.Properties.ValueMember = "MaNV";
            cboNguoiGiaoIT.EditValue = nguoinhan;

            dateNgayGiao.DateTime = DateTime.Now;
          //  dateHanBaoHanh.DateTime = DateTime.Today;
            dateNgayGiaoIT.DateTime = DateTime.Now;
            dtofGiao = Class.CV_BanGiaoLinhKien.DanhsachLKPhongIT();
            gridItemGiao.DataSource = dtofGiao;
            fnc_CreateIndex(gridDetailItemGiao, gridColumn4);

           // cboNCC.Properties.DataSource = Class.MT_MayViTinh.HienthidsDL("MT_NhaCungCap");
           // cboNCC.Properties.DisplayMember = "TenNhaCC";
           // cboNCC.Properties.ValueMember = "MaNhaCC";
            DataTable dttt = Class.MT_MayViTinh.HienthidsDL("MT_TinhTrang");
            cboTinhTrangIT.Properties.DataSource = dttt;
            cboTinhTrangIT.Properties.DisplayMember = "TenTT";
            cboTinhTrangIT.Properties.ValueMember = "IDTT";
            cboTinhTrangIT.EditValue = dttt.Rows[0]["IDTT"].ToString();

        }

        DataTable dtthemlk = new DataTable();
        DataTable dtlk = new DataTable();
        private void cboMaMay_EditValueChanged(object sender, EventArgs e)
        {            
            dtlk=   Class.MT_CTMayViTinh.HienthidsCTMayInfo(cboMaMay.EditValue.ToString());
            gridItem.DataSource = dtlk;
            fnc_CreateIndex(gridDetailItem, ct_colum1);
        }

        private void fnc_CreateIndex(DevExpress.XtraGrid.Views.Grid.GridView gridname, DevExpress.XtraGrid.Columns.GridColumn  columname)
        {

            for (int i = 0; i < gridname.RowCount; i++)
            {
                gridname.SetRowCellValue(i, columname.FieldName, i + 1);
            }
            gridname.BestFitColumns();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int SelectedRow = gridDetailItemGiao.FocusedRowHandle;
            if (SelectedRow >= 0)
            {
                DataRow drow = gridDetailItemGiao.GetDataRow(SelectedRow);
                string _value = drow["MaLK"].ToString();
                string _name = drow["TenLK"].ToString();
                string _nhacc = drow["NhaCC"].ToString();
                string _tinhtrang = drow["TinhTrang"].ToString();
                DateTime _hanbaohanh =DateTime.Parse(drow["HanBaoHanh"].ToString());
                string _id = drow["ID"].ToString();

                DataRow dr = dtlk.NewRow();
                dr[1] = cboMaMay.EditValue.ToString();
                dr[11] = _value;
                dr[12] = _name;
                dtlk.Rows.Add(dr);
                gridItem.DataSource = dtlk;
                fnc_CreateIndex(gridDetailItem, ct_colum1);

                DataRow drthemlk = dtthemlk.NewRow();
                drthemlk[0] = _value;
                drthemlk[1] = _nhacc;
                drthemlk[2] = _hanbaohanh;
                drthemlk[3] = _tinhtrang;
                drthemlk[4] = _id;
                dtthemlk.Rows.Add(drthemlk);

                for (int i = 0; i<dtofGiao.Rows.Count; i++)
                {
                    DataRow dritGiao = dtofGiao.Rows[i];
                    if (dritGiao["ID"].ToString() == _id)
                    {
                        dtofGiao.Rows.Remove(dritGiao);
                        break;
                    }
                }
                gridItemGiao.DataSource = dtofGiao;
            }
            else
            {
                MessageBox.Show("Chưa chọn linh kiện ");

            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (dtthemlk.Rows.Count > 0)
            {
                
                Class.CV_BanGiaoLinhKien bglk= new Class.CV_BanGiaoLinhKien();
                bglk.MaMay= cboMaMay.EditValue.ToString();
                bglk.NgayGiao=dateNgayGiao.DateTime;
                bglk.NguoiNhan=txtMaNguoiNhan.Text;
                bglk.NguoiGiao=cboNguoiGiao.EditValue.ToString();
                bglk.GhiChu = txtGhiChu.Text;
              //  bglk.HanBaoHanh = DateTime.Parse(dateHanBaoHanh.Text);
              //  bglk.NhaCungCap = cboNCC.EditValue.ToString();
                try
                {
                    if (Class.CV_BanGiaoLinhKien.BanGiaoLinhKien(bglk, dtthemlk))
                    {
                        btnLuu.Enabled = false;
                        MessageBox.Show(" Bàn giao thành công ");
                        this.Close();
                    }else
                        MessageBox.Show(" Lỗi . không thêm được dữ liệu bàn giao");
                }
                catch
                {
                    MessageBox.Show(" Lỗi . không thêm được dữ liệu ");
                }

            }
            else
            {
                MessageBox.Show(" Bạn chưa chọn linh kiện bàn giao ");
            }
        }

        private void popupAdd_Click(object sender, EventArgs e)
        {
            
        }

        DataTable dtofIT = new DataTable();
        DataTable dtofGiao = new DataTable();
        private void cboNguoiGiaoIT_EditValueChanged(object sender, EventArgs e)
        {

                DataTable dt = Class.CV_BanGiaoThietBi.HienThiDanhSachBanGiao(cboNguoiGiaoIT.EditValue.ToString());
                if (dt.Rows.Count > 0)
               {
                   cboMayGiao.Properties.DataSource = dt;
                   cboMayGiao.EditValue = dt.Rows[0]["MaMay"].ToString();
                   btnThemIT.Enabled = true;
               }else
                    btnThemIT.Enabled = false;
        }
        DataTable dtnguoigiao = new DataTable();
        DataTable dtIT = new DataTable();
        private void cboMayGiao_EditValueChanged(object sender, EventArgs e)
        {
            dtofIT = Class.MT_CTMayViTinh.HienthidsCTMayInfo(cboMayGiao.EditValue.ToString());
            gridItemGiaoIT.DataSource = dtofIT;
           fnc_CreateIndex(gridDetailGiaoIT, gridColumn18);
        }

        private void btnThemIT_Click(object sender, EventArgs e)
        {
            int SelectedRow = gridDetailGiaoIT.FocusedRowHandle;
            if (SelectedRow >= 0)
            {
                DataRow drow = gridDetailGiaoIT.GetDataRow(SelectedRow);
                string _value = drow["LinhKien"].ToString();
                string _name = drow["TenLK"].ToString();
                string _nhacc = drow["NhaCC"].ToString();
                string _tinhtrang = drow["TinhTrang"].ToString();
                DateTime _hanbaohanh = DateTime.Parse(drow["HanBaoHanh"].ToString());
                string _id = drow["IDChiTiet"].ToString();

                DataRow dr = dtIT.NewRow();
                dr[0] = "0";
                dr[1] = _value;
                dr[2] = _name;
                dr[3] = _nhacc;
                dr[4] = _hanbaohanh;
                dr[5] = _tinhtrang;
                dr[6] = _id;
                dtIT.Rows.Add(dr);
                gridIT.DataSource = dtIT;

                for (int i = 0; i < dtofIT.Rows.Count; i++)
                {
                    DataRow drit = dtofIT.Rows[i];
                    if (drit["IDChiTiet"].ToString() == _id)
                    {
                        dtofIT.Rows.Remove(drit);
                        break;
                    }
                }
                gridItemGiaoIT.DataSource = dtofIT;
                fnc_CreateIndex(gridDetailIT, gridColumn23);
            }
            else
            {
                MessageBox.Show("Chưa chọn linh kiện ");

            }
        }

        private void btnLuuIT_Click(object sender, EventArgs e)
        {
            if (dtIT.Rows.Count > 0)
            {
                Class.CV_BanGiaoLinhKien bglk = new Class.CV_BanGiaoLinhKien();
                bglk.MaMay = cboMayGiao.EditValue.ToString();
                bglk.NgayGiao =dateNgayGiaoIT.DateTime;
                bglk.NguoiNhan = cboNguoiNhanIT.EditValue.ToString();
                bglk.NguoiGiao = cboNguoiGiaoIT.EditValue.ToString();
                bglk.GhiChu = txtGhiChuIT.Text;
                try
                {
                    if (Class.CV_BanGiaoLinhKien.BanGiaoLinhKienChoIT(bglk, dtIT))
                    {
                        btnLuuIT.Enabled = false;
                        MessageBox.Show("Bàn giao thành công ");
                        this.Close();
                    }else
                        MessageBox.Show("Có Lỗi - Bàn giao Thất bại ");

                }
                catch
                {
                    MessageBox.Show("Lỗi - Bàn giao Thất bại ");
                }

            }
            else
            {
                MessageBox.Show("Bạn bàn giao linh kiện nào cả ");
            }
        }

    }
}